<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: InvitationsController.php 2010-07-02 19:54 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Inviter_InvitationsController extends Core_Controller_Action_Standard {
	public function indexAction() {
		if (! $this->_helper->api ()->user ()->getViewer ()->getIdentity ()) {
			$this->_redirect ( 'inviter' );
		}
		
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'inviter_profile', array (), 'inviter_profile_invitation' );
		
		$user = Engine_Api::_ ()->user ()->getViewer ();
		
		// Make paginator
		$table = $this->_helper->api ()->getDbtable ( 'invites', 'inviter' );
		$values ['select'] = $table->select ()->where ( 'user_id = ? && new_user_id = 0', $user->user_id )->order ( 'sent_date Desc' );
		$values ['page'] = $this->_getParam ( 'page', 1 );
		$values ['limit'] = 10;
		
		$this->view->paginator = $paginator = Engine_Api::_ ()->inviter ()->getInviterPaginator ( $values );
		$this->view->paginator = $paginator->setCurrentPageNumber ( $values ['page'] );
		
		$facebook = Inviter_Api_Provider::getFBInstance ();
		$this->view->appId = $facebook->getAppId ();
		$this->view->fb_user_id = Inviter_Api_Provider::getFBUserId ();
	}
	
	public function deleteAction() {
		if (! $this->_helper->api ()->user ()->getViewer ()->getIdentity ()) {
			$this->_redirect ( 'inviter/index/send' );
		}
		
		$id = $this->_getParam ( 'id', null );
		$this->view->invitation = $invitation = Engine_Api::_ ()->inviter ()->getInvitation ( $id );
		$this->view->form = $form = new Inviter_Form_Delete ();
		
		if ($this->getRequest ()->isPost ()) {
			$invitation->delete ();
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'INVITER_Invitation deleted.' ) ) ) );
		}
	}
	
	public function sendnewAction() {
		$viewer = $this->_helper->api ()->user ()->getViewer ();
		
		if (! $viewer->getIdentity ()) {
			$this->_redirect ( 'inviter/index/send' );
		}
		
		$id = $this->_getParam ( 'id', null );
		$this->view->form = $form = new Inviter_Form_Sendnew ( array ('params' => $id ) );
		$this->view->invitation = $invitation = array ('sender' => $form->_inv->sender, 'recipient' => $form->_inv->recipient, 'recipient_name' => $form->_inv->recipient_name, 'provider' => $form->_inv->provider );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getParams () )) {
			$this->_success = $form->sendNewInvite ();
		}
		
		if ($this->getRequest ()->isPost () && ! $form->isErrors ()) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array ($this->_success ) ) );
		}
	}
	
	public function resendAction() {
		$invite_id = $this->_getParam ( 'invite_id', false );
		$viewer = $this->_helper->api ()->user ()->getViewer ();
		$inviterApi = Engine_Api::_ ()->getApi ( 'core', 'inviter' );
		
		$this->view->invitation = $invitation = $inviterApi->getInvitation ( $invite_id );
		$this->view->current_page = $current_page = $this->_getParam ( 'page', '' );
		
		if (! $viewer->getIdentity () || ! $invite_id || ! $invitation) {
			$this->_redirect ( 'inviter/index/send' );
		}
		
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'inviter_profile', array (), 'inviter_profile_invitation' );
		
		$skip = $this->_getParam ( 'skip', false );
		$invite_code = $this->_getParam ( 'code', '' );
		$recipient_id = $this->_getParam ( 'recipient_id', false );
		
		if ($skip) {
			
			if ($recipient_id) {
				$invitation->setFromArray ( array ('code' => $invite_code, 'sent_date' => new Zend_Db_Expr ( 'NOW()' ) ) );
				$invitation->save ();
			}
			
			$this->_redirect ( $this->view->url ( array ('page' => $current_page ), 'inviter_invitations', true ) );
		}
		
		$invite_code = substr ( md5 ( rand ( 0, 999 ) . $viewer->getIdentity () ), 10, 7 );
		
		$host_url = (_ENGINE_SSL ? 'https://' : 'http://') . $_SERVER ['HTTP_HOST'];
		$this->view->action_url = $host_url . $this->view->url ( array ('module' => 'inviter', 'controller' => 'invitations', 'action' => 'resend', 'invite_id' => $invite_id, 'page' => $current_page, 'skip' => 1, 'code' => $invite_code ), 'default', true );
		
		$this->view->invite_url = $host_url . $this->view->url ( array ('module' => 'inviter', 'controller' => 'signup', 'code' => $invite_code ), 'default', true );
		
		if ($invitation->provider == 'facebook') {
			$facebook = Inviter_Api_Provider::getFBInstance ();
			$fb_user_id = Inviter_Api_Provider::getFBUserId ();
			
			$this->view->appId = $facebook->getAppId ();
			$this->view->init_fb_app = $inviterApi->checkInitFbApp ();
			
			if (! $this->view->appId && ! $fb_user_id) {
				return;
			}
			
			if ($fb_user_id && $fb_user_id != $invitation->sender) {
				$this->view->fb_logout = true;
			}
		}
	}
}